package com.exwave.socialgame.model_society.controller;

import com.exwave.socialgame.jwt_token.Token;
import com.exwave.socialgame.model_society.entity.Cityteams;
import com.exwave.socialgame.model_society.service.CityteamsService;
import com.exwave.socialgame.myutils.ReturnUtils;
import com.exwave.socialgame.myutils.WolfGameUtils;
import com.exwave.socialgame.myutils.ParamsException;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * (Cityteams)表控制层
 *
 * @author Ethreal
 * @since 2021-12-13 09:56:51
 */
@RestController
@RequestMapping("cityteams")
public class CityteamsController {

    @Resource
    private CityteamsService cityteamsService;

    /**
     * 通过主键查询单条数据
     * 为后面的评论写的接口
     *
     * @param cid 主键
     * @return 单条数据
     */
    @Token.UserLoginToken
    @ApiOperation("查询一个帖子")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cid", value = "Comment ID", required = true, dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "token", value = "Token", required = false, dataTypeClass = String.class, paramType = "header")
    })
    @GetMapping("selectOne")
    public Cityteams selectOne(Integer cid) {
        return this.cityteamsService.queryById(cid);
    }

    /**
     * 获取评论列表，根据数量查询
     *
     * @param offset 起始位置 0
     * @param limit  查询条数
     */
    @Token.UserLoginToken
    @GetMapping("getList")
    @ApiOperation("获取帖子列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "offset", value = "起始位置", required = true, dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "查询数量", required = true, dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "token", value = "Token", required = false, dataTypeClass = String.class, paramType = "header")
    })
    public Object selectMore(int offset, int limit) {
        HashMap<String, Object> map = new HashMap();
        List<Cityteams> list;
        try {
            list = this.cityteamsService.queryAllByLimit(offset, limit);
        } catch (ParamsException p) {
            return ReturnUtils.returnMap(p.getCode(), p.getMsg());
        } catch (Exception e) {
            return ReturnUtils.returnMap(500, "unknown error");
        }
        map.put("list", list);
        return ReturnUtils.returnMap(200, "success", map);
    }

    @Token.UserLoginToken
    @PostMapping("newPost")
    @ApiOperation("新发布一个帖子")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "Token", required = false, dataTypeClass = String.class, paramType = "header"),
            @ApiImplicitParam(name = "userid", value = "发帖人UID", required = true, dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "title", value = "帖子标题", required = true, dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "themeid", value = "主题分类", required = false, dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "content", value = "详细内容", required = true, dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "pnum", value = "目标集结人数", required = false, dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "longitude", value = "预定位置经度", required = false, dataTypeClass = Double.class, paramType = "query"),
            @ApiImplicitParam(name = "latitude", value = "预定位置维度", required = false, dataTypeClass = Double.class, paramType = "query"),
            @ApiImplicitParam(name = "ptime", value = "预定集结时间", required = false, dataTypeClass = Date.class, paramType = "query")
    })
    public Object newPost(@ApiIgnore Cityteams cityteams) {
        HashMap<String, Object> map = new HashMap();
        try {
            cityteams = this.cityteamsService.insert(cityteams);
        } catch (ParamsException p) {
            return ReturnUtils.returnMap(p.getCode(), p.getMsg());
        } catch (Exception e) {
            return ReturnUtils.returnFailure(e.getMessage());
        }
        map.put("post", cityteams);
        return ReturnUtils.returnSuccess(map);
    }


    @Token.UserLoginToken
    @PostMapping("delPost")
    @ApiOperation(value = "删除帖子")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "Token", required = true, dataTypeClass = String.class, paramType = "header"),
            @ApiImplicitParam(name = "comid", value = "帖子ComID", required = true, dataTypeClass = Integer.class, paramType = "query")
    })
    public Object delPost(int comid) {
        try {
            this.cityteamsService.deleteById(comid);
        } catch (ParamsException p) {
            return WolfGameUtils.returnFailure(p.getCode(), p.getMsg());
        } catch (Exception e) {
            return WolfGameUtils.returnFailure(e.getMessage());
        }
        return WolfGameUtils.returnSuccess();
    }


    @Token.UserLoginToken
    @GetMapping("getUserPost")
    @ApiOperation(value = "获取某人的全部帖子")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户UID", required = true, dataTypeClass = Integer.class, paramType = "query"),
            @ApiImplicitParam(name = "token", value = "Token", required = true, dataTypeClass = String.class, paramType = "header")
    })
    public Object getUserPosts(int uid) {
        List<Cityteams> list = new ArrayList<>();
        try {
            list = this.cityteamsService.queryAllByUID(uid);
        } catch (ParamsException p) {
            return WolfGameUtils.returnFailure(p.getCode(), p.getMsg());
        } catch (Exception e) {
            WolfGameUtils.returnFailure(e.getMessage());
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("posts", list);
        return WolfGameUtils.returnSuccess(map);
    }
}